//
//  SquareDetailsData.m
//
//  Created by lianchuang  on 14-12-17
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "SquareDetailsData.h"
#import "SquareDetailsPost.h"
#import "SquareDetailsThread.h"


NSString *const kSquareDetailsDataPost = @"post";
NSString *const kSquareDetailsDataThread = @"thread";


@interface SquareDetailsData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SquareDetailsData

@synthesize post = _post;
@synthesize thread = _thread;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
    NSObject *receivedSquareDetailsPost = [dict objectForKey:kSquareDetailsDataPost];
    NSMutableArray *parsedSquareDetailsPost = [NSMutableArray array];
    if ([receivedSquareDetailsPost isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSquareDetailsPost) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSquareDetailsPost addObject:[SquareDetailsPost modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSquareDetailsPost isKindOfClass:[NSDictionary class]]) {
       [parsedSquareDetailsPost addObject:[SquareDetailsPost modelObjectWithDictionary:(NSDictionary *)receivedSquareDetailsPost]];
    }

    self.post = [NSMutableArray arrayWithArray:parsedSquareDetailsPost];
            self.thread = [SquareDetailsThread modelObjectWithDictionary:[dict objectForKey:kSquareDetailsDataThread]];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForPost = [NSMutableArray array];
    for (NSObject *subArrayObject in self.post) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForPost addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForPost addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForPost] forKey:kSquareDetailsDataPost];
    [mutableDict setValue:[self.thread dictionaryRepresentation] forKey:kSquareDetailsDataThread];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.post = [aDecoder decodeObjectForKey:kSquareDetailsDataPost];
    self.thread = [aDecoder decodeObjectForKey:kSquareDetailsDataThread];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_post forKey:kSquareDetailsDataPost];
    [aCoder encodeObject:_thread forKey:kSquareDetailsDataThread];
}

- (id)copyWithZone:(NSZone *)zone
{
    SquareDetailsData *copy = [[SquareDetailsData alloc] init];
    
    if (copy) {

        copy.post = [self.post copyWithZone:zone];
        copy.thread = [self.thread copyWithZone:zone];
    }
    
    return copy;
}


@end
